import { certTypes, degrees, learnWays, sexs, subjects } from './params'
import { enroll, certPay } from '../../../api/cert'
import { sendValidCode } from '../../../api/base'
import { userGetPhoneNumber } from '../../../api/user'

Page({
  data: {
    certTypes: certTypes,
    degrees: degrees,
    learnWays: learnWays,
    sexs: sexs,
    subjects: subjects,
    subjectIndex: 0,
    form: {
      phone: '',
      smsCode: '',
      cardNum: '',
      certType: '',
      degree: '',
      learnWay: '',
      reading: true,
      realName: '',
      school: '',
      sex: '',
      startYear: null,
      subSchool: '',
      subject: ''
    },
    // 报名
    certType: '',
    showCertType: false,
    // 学科
    subject: '',
    showSubject: '',
    // 性别
    sex: '',
    showSex: false,
    // 学历
    degree: '',
    showDegree: false,
    // 入学时间
    startYear: new Date().getTime(),
    showStartYear: false,
    // 学习形式
    learnWay: '',
    showLearnWay: false,
    showInfo: false,
    showSuccess: false,
    isCountDown: false,
    time: 60 * 1000,
    showTips: false
  },
  onLoad: function (options) {

  },
  /** 手机号码 start **/
  getPhoneNumber(e) {
    const _this = this
    wx.checkSession({
      success() {
        _this.setPhoneNumber(e.detail)
      },
      fail() {
        wx.login({
          success(res) {
            _this.setPhoneNumber(e.detail)
          }
        })
      }
    })
  },

  setPhoneNumber(detail) {
    userGetPhoneNumber({
      encryptedData: detail.encryptedData,
      iv: detail.iv
    }).then(res => {
      console.log(res)
      this.setData({
        'form.mobileNumber': res
      })
    })
  },

  changePhone({ detail = {} }) {
    if (/^1[3456789]\d{9}$/.test(detail.value)) {
      this.setData({
        'form.phone': detail.value
      })
    } else {
      wx.showToast({
        title: '请输入正确的手机号码',
        icon: 'none',
        duration: 2000
      })
    }
  },

  changeVerifyCode({ detail = {} }) {
    this.setData({
      'form.smsCode': detail.value
    })
  },

  getValidCode() {
    sendValidCode({
      phone: this.data.form.phone,
      bizTypeEnums: '考证报名'
    }).then(res => {
      wx.showToast({
        title: '短信已发送'
      })
      this.countDownStart()
    })
  },

  countDownStart() {
    this.setData({
      isCountDown: true
    })
    const countDown = this.selectComponent('.control-count-down');
    countDown.start();
  },

  countDownReset() {
    // const countDown = this.selectComponent('.control-count-down');
    // countDown.reset();
    this.setData({
      isCountDown: false
    })
  },
  /** 手机号码 end **/
  /** 报考类型 start **/
  confirmCertType(e) {
    this.setData({
      certType: e.detail.name,
      'form.certType': e.detail.value
    })
  },
  selectCertType() {
    this.setData({ showCertType: true })
  },
  cancelSelectCertType() {
    this.setData({ showCertType: false })
  },
  /** 报考类型 end **/

  /** 学科 start **/

  selectSubject() {
    this.setData({showSubject: true})
  },

  cancelSelectSubject() {
    this.setData({showSubject: false})
  },

  selectSubjectNav({ detail = {} }){
    this.setData({
      subjectIndex: detail.index || 0,
    });
  },

  checkSubject(e) {
    const index = e.currentTarget.dataset.index
    let subjects = this.data.subjects;
    subjects.forEach(item => {
      item.children.forEach(child => {
        child.checked = false
      })
    })
    subjects[this.data.subjectIndex].children[index].checked = true
    this.setData({
      subjects: subjects
    })
  },

  confirmSelectSubject() {
    const _this = this
    const subjects = this.data.subjects
    subjects.forEach(item => {
      item.children.forEach(child => {
        if (child.checked) {
          _this.setData({
            showSubject: false,
            subject: child.name,
            'form.subject': child.value
          })
        }
      })
    })
  },
  /** 学科 end **/

  /** 姓名 start **/
  changeName({ detail = {} }) {
    this.setData({
      'form.realName': detail.value
    })
  },
  /** 姓名 end **/

  /** 性别 start **/
  confirmSex(e) {
    this.setData({
      sex: e.detail.name,
      'form.sex': e.detail.value
    })
  },
  selectSex() {
    this.setData({ showSex: true })
  },
  cancelSelectSex() {
    this.setData({ showSex: false })
  },
  /** 报考类型 end **/

  /** 身份证 start **/
  changeCardNum({ detail = {} }) {
    this.setData({
      'form.cardNum': detail.value
    })
  },
  /** 身份证 end **/

  /** 学历 start **/
  confirmDegree(e) {
    this.setData({
      degree: e.detail.name,
      'form.degree': e.detail.value
    })
  },
  selectDegree() {
    this.setData({ showDegree: true })
  },
  cancelSelectDegree() {
    this.setData({ showDegree: false })
  },
  /** 学历 end **/

  /** 毕业学校 start **/
  changeSchool({ detail = {} }) {
    this.setData({
      'form.school': detail.value
    })
  },
  /** 毕业学校 end **/

  /** 所在校区 start **/
  changeSubSchool({ detail = {} }) {
    this.setData({
      'form.subSchool': detail.value
    })
  },
  /** 所在校区 end **/

  /** 入学时间 start **/
  selectStartYear() {
    this.setData({ showStartYear: true })
  },
  cancelSelectStartYear() {
    this.setData({ showStartYear: false })
  },
  confirmStartYear(e) {
    this.setData({
      startYear: e.detail,
      'form.startYear': new Date(e.detail).getFullYear(),
      showStartYear: false
    })
  },
  /** 入学时间 end **/

  /** 学习形式 start **/
  confirmLearnWay(e) {
    this.setData({
      learnWay: e.detail.name,
      'form.learnWay': e.detail.value
    })
  },
  selectLearnWay() {
    this.setData({ showLearnWay: true })
  },
  cancelSelectLearnWay() {
    this.setData({ showLearnWay: false })
  },
  /** 学习形式 end **/

  /** 是否在读 start **/
  changeReading(e) {
    this.setData({
      'form.reading': e.detail
    })
  },
  /** 是否在读 end **/
  confirmResearch() {
    this.setData({ showInfo: true })
  },
  closeInfo() {
    this.setData({ showInfo: false })
  },
  confirmEnroll() {
    const form = this.data.form;
    if (!form.smsCode || !form.cardNum || !form.certType || !form.degree || !form.learnWay || !form.realName || !form.school || !form.sex || !form.subSchool || !form.subject) {
      wx.showToast({
        title: '请先填写完整资料',
        icon: 'none',
        duration: 3000
      })
      return false;
    }
    enroll(this.data.form).then(res => {
      // this.pay(res)
      this.closeInfo()
      this.setData({ showTips: true })
    })
  },
  pay(orderId) {
    return new Promise(((resolve, reject) => {
      certPay(orderId).then(res => {

      })
    }))
  },
  joinedTextual() {
    this.setData({ showTips: false })
    wx.navigateTo({
      url: '/packageA/pages/textualResearchList/textualResearchList'
    });
  },
  close() {
    this.setData({ showTips: false })
  }
})
